Imports System.Data.OleDb

Imports TaxonNamesInterface

Public Class AuthorManager

    Public Delegate Sub ImportStatusDelegate(ByVal percComplete As Integer)
    Public Shared ImportStatusCallback As ImportStatusDelegate

    Public Shared Sub ImportAuthors(ByVal mdbFilename As String)
        Dim cnn As OleDbConnection
        Dim ds As New DataSet

        'load data from mdb
        Try
            cnn = New OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + mdbFilename + ";")
            cnn.Open()

            Using cmd As OleDbCommand = cnn.CreateCommand()
                Dim da As New OleDbDataAdapter(cmd)

                cmd.CommandText = "select * from tblAuthors"
                da.Fill(ds)
            End Using

        Catch ex As Exception
            NamesException.LogError(ex)
        Finally
            If cnn IsNot Nothing Then cnn.Close()
        End Try


        If ds.Tables.Count > 0 Then
            Dim count As Integer = ds.Tables(0).Rows.Count
            Dim pos As Integer = 1

            For Each row As DataRow In ds.Tables(0).Rows
                
                NamesClient.AuthorsInterfaceProxy.SaveAuthor(AuthorFromRow(row), SessionState.CurrentUser.Login)

                Dim p As Integer = pos * 100 / count
                If p > 99 Then p = 99 'dont finsih till really done
                If ImportStatusCallback IsNot Nothing Then ImportStatusCallback.Invoke(p)
                pos += 1
            Next
            'done
            If ImportStatusCallback IsNot Nothing Then ImportStatusCallback.Invoke(100)
        End If
    End Sub

    Public Shared Sub SaveAuthors(ByVal ds As DataSet)
        Dim ch As DataTable = ds.Tables(0).GetChanges()
        Dim origCh As DataTable = ch.Copy()

        'do new authors first
        For Each row As DataRow In ch.Rows
            If row("AuthorPk") < 0 Then
                Dim oldId As Integer = row("AuthorPk")

                row("AuthorPk") = -1

                NamesClient.AuthorsInterfaceProxy.SaveAuthor(AuthorFromRow(row), SessionState.CurrentUser.Login)

                'update dataset
                Dim oldRow As DataRow() = ds.Tables(0).Select("AuthorPk = " + oldId.ToString())
                If oldRow.Length > 0 Then oldRow(0)("AuthorPk") = row("AuthorPk")

                'update other authors that pointed to this one
                For Each r As DataRow In ch.Rows
                    If r("CorrectAuthorFk") = oldId Then r("CorrectAuthorFk") = row("AuthorPk")
                Next
            End If
        Next

        'do correct author updates
        For Each row As DataRow In origCh.Rows
            If row("AuthorPk") > 0 Then
                NamesClient.AuthorsInterfaceProxy.SaveAuthor(AuthorFromRow(row), SessionState.CurrentUser.Login)
            End If
        Next
    End Sub

    Private Shared Function AuthorFromRow(ByVal row As DataRow) As Author
        Dim a As New Author
        a.Abbreviation = row("Abbreviation").ToString
        a.AuthorId = row("AuthorId")
        a.ChangeDate = row("ChangeDate")
        a.ChangeUser = row("ChangeUser").ToString
        a.CorrectAuthorId = row("CorrectAuthorId")
        a.Dates = row("Dates").ToString
        a.Forename = row("Forename").ToString
        a.IPNIAlternativeNames = row("IPNIAlternativeNames").ToString
        a.IPNIAuthor_id = row("IPNIAuthor_id").ToString
        a.IPNIVersion = row("IPNIVersion").ToString
        a.Surname = row("Surname").ToString
        a.TaxonGroups = row("TaxonGroups").ToString
        Return a
    End Function
End Class
